﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Media;
using System.IO;

namespace Enter.Common
{
    public class Utils
    {

        #region Close
        /// <summary>
        /// 
        /// </summary>
        public static void CloseWin()
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("<script language=\"javascript\">");
            builder.Append("window.close();");
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        #endregion

        #region throwErrorAndClose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="err"></param>
        public static void throwErrorAndClose(string err)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder("");

            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("window.close();");
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        #endregion

        #region throwErrorGotoURL
        /// <summary>
        /// 
        /// </summary>
        /// <param name="err"></param>
        /// <param name="url"></param>
        public static void throwErrorGotoURL(string err, string url)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("location='" + url + "';");
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        #endregion

        #region ShowConfirm
        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="yes_method"></param>
        /// <param name="no_method"></param>
        public static void ShowConfirm(string msg, string yes_method, string no_method)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("<script language=\"javascript\">");
            builder.AppendFormat("var flag = confirm('{0}');\r\n", msg);
            builder.Append("if(flag){\r\n");
            builder.Append(yes_method);
            builder.Append("\r\n}else{\r\n");
            builder.Append(no_method);
            builder.Append("}\r\n;");
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        #endregion

        #region GotoURL
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        public static void GotoURL(string url)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("<script language=\"javascript\">");
            builder.AppendFormat("location=\"{0}\";", url);
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        #endregion

        #region 执行JS
        /// <summary>
        /// 
        /// </summary>
        /// <param name="js"></param>
        public static void ExecuteJs(string js)
        {
            HttpContext.Current.Response.Write(string.Format("<SCRIPT language=\"javascript\">{0}</script>", js));
        }
        #endregion

        #region ShowMessage
        /// <summary>
        /// 显示提示信息，影响CSS
        /// </summary>
        /// <param name="err"></param>
        public static void ShowMessage(string err)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("</script>");
            HttpContext.Current.Response.Write(builder.ToString());
        }

        /// <summary>
        /// 显示提示信息，不影响CSS
        /// </summary>
        /// <param name="page"></param>
        /// <param name="err"></param>
        public static void ShowMessage(System.Web.UI.Page page, string err)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("</script>");
            if (page.ClientScript.IsStartupScriptRegistered("msgAlert"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "msgAlert", builder.ToString());
            }
            else
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "msgAlert", builder.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="agrs"></param>
        public static void ShowMessage(string format, params object[] agrs)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script language=\"javascript\">\r\n");
            builder.Append("alert('");
            builder.AppendFormat(format, agrs);
            builder.Append("')");
            builder.Append("\r\n</script>");
            HttpContext.Current.Response.Write(builder.ToString());
        }
        #endregion

        #region throwErrorBack
        /// <summary>
        /// 
        /// </summary>
        /// <param name="err"></param>
        public static void throwErrorBack(string err)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + err + "\");");
            builder.Append("history.go(-1);");
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void throwErrorBack(string format, params string[] args)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            builder.Append("<script language=\"javascript\">");
            builder.Append("alert(\"" + string.Format(format, args) + "\");");
            builder.Append("history.go(-1);");
            builder.Append("</script>");

            HttpContext.Current.Response.Write(builder.ToString());
            HttpContext.Current.Response.End();
        }
        #endregion

        /// <summary>
        /// 执行JS，不影响CSS
        /// </summary>
        /// <param name="js"></param>
        public static void ExecuteJs(System.Web.UI.Page page, string js)
        {
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            builder.Append("<script language=\"javascript\">");
            builder.Append(js);
            builder.Append("</script>");
            if (page.ClientScript.IsStartupScriptRegistered("msgAlert"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "msgAlert", builder.ToString());
            }
            else
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "msgAlert", builder.ToString());
            }
        }
        #region 播放语音文件
        public static void PlaySound()
        {
            try
            {

                SoundPlayer sp = new SoundPlayer(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "alert.wav"));
                sp.Play();
            }
            catch
            { }
        }
        #endregion

    }
}
